//
//  UIHelper.m
//  EASMobile
//
//  Created by yzk on 15-7-21.
//  Copyright (c) 2015年 ABB. All rights reserved.
//

#import "UIHelper.h"

@implementation UIHelper

+ (MBProgressHUD *)showIndicatorToView:(UIView *)v
{
    return [MBProgressHUD showHUDAddedTo:v animated:YES];
}

+ (MBProgressHUD *)showIndicatorWithText:(NSString *)text ToView:(UIView *)v
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:v animated:YES];

    hud.labelText = text;
    hud.mode = MBProgressHUDModeIndeterminate;
    return hud;
}


+ (MBProgressHUD *)showText:(NSString *)text ToView:(UIView *)v
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:v animated:YES];

    hud.labelText = text;
    hud.mode = MBProgressHUDModeText;
    hud.margin = 15.f;
    [hud hide:YES afterDelay:1];
    return hud;
}


+ (MBProgressHUD *)showDetailText:(NSString *)text ToView:(UIView *)v
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:v animated:YES];
    
    hud.detailsLabelText= text;
    hud.mode = MBProgressHUDModeText;
    hud.margin = 15.f;
    [hud hide:YES afterDelay:1];
    return hud;
}

+ (void)hideHUDWithCompletedText:(NSString *)text forView:(UIView *)view second:(int) second
{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];

    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    hud.mode = MBProgressHUDModeCustomView;
    hud.labelText = text;
    [hud hide:YES afterDelay:second];
}

+ (void)hideSingleHUDForView:(UIView *)view
{
    [MBProgressHUD hideHUDForView:view animated:NO];
}

+ (void)hideHUDForView:(UIView *)view
{
    [MBProgressHUD hideAllHUDsForView:view animated:YES];
}

+ (UIAlertView *)alertWithMsg:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
    return alert;
}

+ (UIAlertView *)alertWithTitle:(NSString *)title andMsg:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
    return alert;
}

+ (BOAlertController *)alertWithMsg:(NSString *)msg viewController:(UIViewController *)viewController action:(void (^)(void))action
{
    BOAlertController *alert = [[BOAlertController alloc] initWithTitle:@"" message:msg viewController:viewController];
    RIButtonItem *item = [RIButtonItem itemWithLabel:@"OK"
                                                type:RIButtonItemType_Cancel
                                              action:action];

    [alert addButton:item];
    [alert show];
    return alert;
}

+ (BOAlertController *)alertWithMsg:(NSString *)msg viewController:(UIViewController *)viewController confirmAction:(void (^)(void))confirmAction cancelAction:(void (^)(void))cancelAction
{
    BOAlertController *alert = [[BOAlertController alloc] initWithTitle:@"" message:msg viewController:viewController];
    RIButtonItem *confirmItem = [RIButtonItem itemWithLabel:@"sure"
                                                       type:RIButtonItemType_Other
                                                     action:confirmAction];
    RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"cancel"
                                                      type:RIButtonItemType_Cancel
                                                    action:cancelAction];

    [alert addButton:confirmItem];
    [alert addButton:cancelItem];
    [alert show];
    return alert;
}

+ (BOAlertController *)alertWithTitle:(NSString *)title msg:(NSString *)msg viewController:(UIViewController *)viewController action:(void (^)(void))action
{
    BOAlertController *alert = [[BOAlertController alloc] initWithTitle:title message:msg viewController:viewController];
    RIButtonItem *item = [RIButtonItem itemWithLabel:@"OK"
                                                type:RIButtonItemType_Cancel
                                              action:action];

    [alert addButton:item];
    [alert show];
    return alert;
}

+ (BOAlertController *)alertWithTitle:(NSString *)title msg:(NSString *)msg viewController:(UIViewController *)viewController confirmAction:(void (^)(void))confirmAction cancelAction:(void (^)(void))cancelAction
{
    BOAlertController *alert = [[BOAlertController alloc] initWithTitle:title message:msg viewController:viewController];
    RIButtonItem *confirmItem = [RIButtonItem itemWithLabel:@"Change now"
                                                       type:RIButtonItemType_Other
                                                     action:confirmAction];
    RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"Back"
                                                      type:RIButtonItemType_Cancel
                                                    action:cancelAction];

    [alert addButton:confirmItem];
    [alert addButton:cancelItem];
    [alert show];
    return alert;
}
+ (void)remindView:(NSString *)message action:(void (^)(void))action
{
    
    
    UIView * showView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 50)];
    showView.center=[UIApplication sharedApplication].keyWindow.center;
    showView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    showView.layer.cornerRadius = 8;
    showView.layer.masksToBounds=YES;
    [[UIApplication sharedApplication].keyWindow addSubview:showView];
    
    
    UILabel * showLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 30)];
    showLable.center=CGPointMake(showView.bounds.size.width/2, showView.bounds.size.height/2);
    showLable.textAlignment=NSTextAlignmentCenter;
    showLable.text=message;
    showLable.font=[UIFont systemFontOfSize:13];
    showLable.textColor=[UIColor whiteColor];
    [showView addSubview:showLable];
    
    [UIView animateWithDuration:1 animations:^{
        showView.alpha=0;
    }completion:^(BOOL finish){
        
        [showView removeFromSuperview];
        if (action) {
            action();
        }
    }];
    
}

@end
